package bairui.international.dao.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.orm.jpa.vendor.Database;

import com.lianjing.sh.project.config.data.jpa.properties.Project_Properties_Data_Hibernate;
import com.lianjing.sh.project.config.data.jpa.properties.Project_Properties_Data_Jdbc;
import com.lianjing.sh.project.config.data.jpa.properties.Project_Properties_Data_Proxool;

@Configuration
@Import({
	Project_Properties_Data_Proxool.class,
	Project_Properties_Data_Jdbc.class,
	Project_Properties_Data_Hibernate.class,
})
public class Config_Data_DataSource {
	
	
	/**
	 * 在数据源配置完后后执行
	 */
	public static interface DataSourceAfterExecute {
		void execute(DataSource dataSource) throws Exception;
	}
	
	@Bean
	DataSourceAfterExecute defaultDataSourceAfterExecute(){
		return new DataSourceAfterExecute(){
			@Override public void execute(DataSource dataSource) {
			}
		};
	}
	
	@Bean @Autowired
	DataSource dataSource(
		Project_Properties_Data_Proxool config_proxool,
		Project_Properties_Data_Jdbc config_jdbc,
		List<DataSourceAfterExecute> dataSourceAfterExecutes
	) throws Exception {
		ProxoolDataSource proxoolDataSource = new ProxoolDataSource();
			proxoolDataSource.setAlias(config_proxool.getProxool_alias());
			proxoolDataSource.setDriverUrl(config_jdbc.getDb_jdbc_url());
			proxoolDataSource.setDriver(config_jdbc.getDb_jdbc_driverClassName());
			// 数据库连接帐号
			proxoolDataSource.setUser(config_jdbc.getDb_jdbc_username());
			proxoolDataSource.setPassword(config_jdbc.getDb_jdbc_password());
			//
			proxoolDataSource.setHouseKeepingSleepTime(config_proxool.getProxool_houseKeepingSleepTime());
			proxoolDataSource.setPrototypeCount(config_proxool.getProxool_prototypeCount());
			proxoolDataSource.setSimultaneousBuildThrottle(config_proxool.getProxool_simultaneousBuildThrottle());
			proxoolDataSource.setMaximumConnectionCount(config_proxool.getProxool_maximumConnectionCount());
			proxoolDataSource.setMaximumActiveTime(ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_ACTIVE_TIME*3);
			proxoolDataSource.setMinimumConnectionCount(config_proxool.getProxool_minimumConnectionCount());
			//
			proxoolDataSource.setHouseKeepingTestSql(config_proxool.getProxool_houseKeepingTestSql());
			proxoolDataSource.setTestBeforeUse(config_proxool.isProxool_testBeforeUse());
			proxoolDataSource.setTestAfterUse(config_proxool.isProxool_testAfterUse());
			//
			for(DataSourceAfterExecute dataSourceAfterExecute : dataSourceAfterExecutes){
				dataSourceAfterExecute.execute(proxoolDataSource);
			}
		return proxoolDataSource;
	}
	
	/**
	 * JPA属性
	 */
	@Bean @Primary @Autowired
	JpaProperties jpaConfig(Project_Properties_Data_Hibernate config_hibernate){
		Map<String, String> map = new HashMap<>();
		//缓存
	//	map.put("hibernate.cache.use_second_level_cache", "true");
	//	map.put("hibernate.cache.use_query_cache", "true");
	//	map.put("generate_statistics", "true");
	//	map.put("hibernate.cache.region.factory_class","org.hibernate.cache.ehcache.EhCacheRegionFactory");
		map.put("hibernate.hbm2ddl.auto", config_hibernate.getHibernate_hbm2ddl_auto());
		map.put("hibernate.dialect", config_hibernate.getHibernate_dialect());
		map.put("hibernate.show_sql", config_hibernate.getHibernate_show_sql().toString());
		map.put("hibernate.use_sql_comments", config_hibernate.getHibernate_use_sql_comments().toString());
		map.put("hibernate.format_sql", config_hibernate.getHibernate_format_sql().toString());
//		map.put("hibernate.max_fetch_depth", "2");
		//
		JpaProperties jpaProperties = new JpaProperties();
		jpaProperties.setProperties(map);
		jpaProperties.setGenerateDdl(false);
		jpaProperties.setDatabase(Database.MYSQL);
		return jpaProperties;
	}
	
	/**
	 * 为@PropertySource方式下使用@Value功能，
	 * 因为使用<context:property-placeholder/>方式会自动开启注入PropertySourcesPlaceholderConfigurer，
	 * 否则不会被注入，所以要手动配置注入PropertySourcesPlaceholderConfigurer
	 */
	@Bean
	PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

}